package com.eascs.admin.o2o.internal.dao;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.form.LoanapplyForm;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;

/**
 * 
 * 收取保证金与管理费
 *
 */
@Repository
public class LoanapplyExpandDao extends BaseO2oDao {
	/**
	 * 根据主表ID 查询收取保证金与管理费
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	public LoanapplyExpand queryLoanapplyNo(String loanapplyNo) {
		String sql = "select * from o2o_loanapply_expand t where t.loanapply_no=?";
		return this.findEntity(sql, LoanapplyExpand.class, loanapplyNo.trim());
	}

	/**
	 * 提交保证金、风险补偿金、管理费 修改状态
	 * 
	 * @param loanapplyExpand
	 */
	public int updateWorkFlow(LoanapplyExpand loanapplyExpand) {
		return this.updateById(loanapplyExpand);
	}

	/**
	 * 修改保证金、风险补偿金、管理费 修改状态
	 * 
	 * @param manger
	 * @param loanapply
	 * @param loanapplyForm
	 * @param recRisk
	 */
	public void updateLoanapplyExpand(BigDecimal manger, O2oLoanapplyEntity loanapply, LoanapplyForm loanapplyForm, BigDecimal recRisk, LoanapplyExpand loanapplyExpand) {
		String loapplySql = "update o2o_loanapply_expand t set t.rec_manger=?,t.rec_deposite=?,t.rec_risk=?,t.depositrate=?,t.managefeerate=?,t.risk=?,t.remark=?,t.payment_type=?,t.generalrate=?,t.bank_loan_name=? where t.id=?";
		this.execute(loapplySql, manger, (loanapplyForm.getEaMount()).multiply((loanapplyForm.getDepositrate())), recRisk, loanapplyForm.getDepositrate(), loanapplyForm.getManagefeerate(), loanapplyForm.getRisk(), loanapplyForm.getRemark(), loanapplyForm.getPaymentType(),
				loanapplyForm.getGeneralRate(), loanapplyForm.getBankLoanName(), loanapplyExpand.getId());
	}

	/**
	 * 根据贷款BillCode查询收取保证金
	 * 
	 * @param billCode
	 * @return
	 */
	public LoanapplyExpand findBillCode(String billCode) {
		String sql = "select * from o2o_loanapply_expand t where t.bill_code=?";
		return this.findEntity(sql, LoanapplyExpand.class, billCode);
	}
	/**
	 * 查询可划拨保证金单号
	 * @param loanId
	 * @return
	 */
	public List<LoanapplyExpand> findLoanId(String loanId) {
		String sql = "SELECT Y.* FROM O2O_LOANAPPLY T,O2O_LOANAPPLY_EXPAND Y WHERE T.LOANAPPLY_NO=Y.LOANAPPLY_NO AND T.LOAN_ID=? AND Y.BILL_STATUS='CLO'";
		return this.findEntitys(sql, LoanapplyExpand.class, loanId);
	}
}
